package com.psds.quartz;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.UUID;

/**
 * author: cjianquan
 * date: 2016/9/1
 */
@Service("quartzLogService")
public class QuartzLogService {
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    public int queryQuartzLogCount(QuartzLogEntity quartzLog,String logExectime_begin,String logExectime_end){
        QuartzLogEntityMapper mapper = this.sqlSessionTemplate.getMapper(QuartzLogEntityMapper.class);
        QuartzLogEntityExample example = new QuartzLogEntityExample();
        QuartzLogEntityExample.Criteria criteria = example.createCriteria();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try{
            if(logExectime_begin!=null && !logExectime_begin.equals("")){
                criteria.andLogExectimeGreaterThanOrEqualTo(sdf.parse(logExectime_begin));
            }
            if(logExectime_end!=null && !logExectime_end.equals("")){
                criteria.andLogExectimeLessThanOrEqualTo(sdf.parse(logExectime_end));
            }
        }catch (ParseException e){
            e.printStackTrace();
        }

        if(quartzLog.getLogTaskname()!=null && !quartzLog.getLogTaskname().equals("")){
            criteria.andLogTasknameLike("%"+quartzLog.getLogTaskname()+"%");
        }

        if(quartzLog.getLogExectype()!=null && !quartzLog.getLogExectype().equals("")){
            criteria.andLogExectypeEqualTo(quartzLog.getLogExectype());
        }

        if(quartzLog.getLogExecresult()!=null && !quartzLog.getLogExecresult().equals("")){
            criteria.andLogExecresultEqualTo(quartzLog.getLogExecresult());
        }
        return mapper.countByExample(example);
    }


    public List<QuartzLogEntity> queryQuartzLogList(QuartzLogEntity quartzLog,int start,int offset,String logExectime_begin,String logExectime_end){
        QuartzLogEntityMapper mapper = this.sqlSessionTemplate.getMapper(QuartzLogEntityMapper.class);
        QuartzLogEntityExample example = new QuartzLogEntityExample();
        QuartzLogEntityExample.Criteria criteria = example.createCriteria();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try{
            if(logExectime_begin!=null && !logExectime_begin.equals("")){
                criteria.andLogExectimeGreaterThanOrEqualTo(sdf.parse(logExectime_begin));
            }
            if(logExectime_end!=null && !logExectime_end.equals("")){
                criteria.andLogExectimeLessThanOrEqualTo(sdf.parse(logExectime_end));
            }
        }catch (ParseException e){
            e.printStackTrace();
        }

        if(quartzLog.getLogTaskname()!=null && !quartzLog.getLogTaskname().equals("")){
            criteria.andLogTasknameLike("%"+quartzLog.getLogTaskname()+"%");
        }

        if(quartzLog.getLogExectype()!=null && !quartzLog.getLogExectype().equals("")){
            criteria.andLogExectypeEqualTo(quartzLog.getLogExectype());
        }

        if(quartzLog.getLogExecresult()!=null && !quartzLog.getLogExecresult().equals("")){
            criteria.andLogExecresultEqualTo(quartzLog.getLogExecresult());
        }
        example.setOrderByClause(" log_exectime desc ");

        example.setStart(start);
        example.setEnd(offset);
        return mapper.selectByExample(example);
    }

    public void saveQuartzLog(QuartzLogEntity quartzLog){
        QuartzLogEntityMapper mapper = this.sqlSessionTemplate.getMapper(QuartzLogEntityMapper.class);
        if(quartzLog.getId()==null || quartzLog.getId().equals("")){
            quartzLog.setId(UUID.randomUUID().toString().replaceAll("-",""));
            mapper.insertSelective(quartzLog);
        }else {
            mapper.updateByPrimaryKeySelective(quartzLog);
        }

    }
}
